package com.datatest.riskcheck.service.serviceImpl;

import com.datatest.beans.po.tongdun.DataCollectorEntity;
import com.datatest.beans.po.tongdun.DecisionFlowEntity;
import com.datatest.riskcheck.dao.tongdun.DataCollectorMapper;
import com.datatest.riskcheck.dao.tongdun.DecisionFlowMapper;
import com.datatest.riskcheck.service.api.QuerySystemApiService;
import com.datatest.riskcheck.tools.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.zip.GZIPInputStream;

/**
 * @Author lxt
 * Created by Jccfc on 2021/10/12.
 */
@Service
public class QuerySystemApiServiceImpl implements QuerySystemApiService {
    @Autowired
    DecisionFlowMapper decisionFlowMapper;
    @Autowired
    DataCollectorMapper dataCollectorMapper;


    @Override
    public List displayNameList(String appName, String eventId) throws IOException {
        DecisionFlowEntity decisionFlowEntity = decisionFlowMapper.selectByEventId(appName,eventId);
        List<String> list = new ArrayList<>();
        InputStream inputStream = null;
        try {
            if (decisionFlowEntity != null){
                String diagramContent = decisionFlowEntity.getDiagramContent();
                //获取指定编码格式的解密后的字节流
                byte[] bytes = Base64Utils.decodeFromString(diagramContent);
                //获取解压后的字节流
                inputStream = new GZIPInputStream(new ByteArrayInputStream(bytes));
                //获取content内容
                String[] byXpathReadXml = FileUtils.getByXpathReadXml(inputStream, "//@content");
                list.addAll(Arrays.asList(byXpathReadXml));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream !=null){
                inputStream.close();
            }
        }
        return list;
    }

    @Override
    public List<DataCollectorEntity> selectByDisplayName(List displayNameList) {
        return dataCollectorMapper.selectByDisplayName(displayNameList);
    }

}
